package com.wasion.pwportable.protocol.analyzer.terminal;

import com.wasion.pwportable.protocol.analyzer.AbstractAnalyzer;

public class Terminal_Version_Analyzer extends AbstractAnalyzer {

	@Override
	public void dataAnalysis(String ackBS) {
		String dataType = ackBS.substring(DATATPYESTARTBIT, DATATPYENDBIT);

		if (dataType.equalsIgnoreCase("DA")) {
			String version = ackBS.substring(DATABITSTART, DATABITSTART + 28);

			result.setSwVersion(getSwVersion(version));
			result.setSwDate(getSwDate(version));
			result.setHwVersion(getHwVersion(version));
			result.setHwDate(getHwDate(version));
			// TBC
		}
	}

	private String getHwDate(String version) {
		// 硬件发布日期
		String data = version.substring(22, 28);
		if ("EEEEEE".equalsIgnoreCase(data)) {
			return "";
		}
		String wDate = data.substring(4, 6) + data.substring(2, 4)
				+ data.substring(0, 2);
		return wDate;
	}

	private String getHwVersion(String version) {
		// 硬件版本号
		String data = version.substring(14, 22);
		if ("EEEEEEEE".equalsIgnoreCase(data)) {
			return "";
		}
		String hwVer = (char) Integer.parseInt(data.substring(0, 2), 16) + ""
				+ (char) Integer.parseInt(data.substring(2, 4), 16) + ""
				+ (char) Integer.parseInt(data.substring(4, 6), 16) + ""
				+ (char) Integer.parseInt(data.substring(6, 8), 16);
		return hwVer.toString();
	}

	private String getSwDate(String version) {
		// 软件发布日期
		String data = version.substring(8, 14);
		String swDate = data.substring(4, 6) + data.substring(2, 4)
				+ data.substring(0, 2);

		return swDate;
	}

	private String getSwVersion(String version) {
		// 软件版本号
		String data = version.substring(0, 8);
		String swVer = (char) Integer.parseInt(data.substring(0, 2), 16) + ""
				+ (char) Integer.parseInt(data.substring(2, 4), 16) + ""
				+ (char) Integer.parseInt(data.substring(4, 6), 16) + ""
				+ (char) Integer.parseInt(data.substring(6, 8), 16);

		return swVer.trim();
	}

}